package com.store.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.store.app.BaseActivity;
import com.store.app.ExitApplication;
import com.store.app.R;
import com.store.app.adapter.SpecialSellRVAdapter;
import com.store.app.bean.SpecialsellDeviceBean;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 特卖页面
 */
public class SpecialSell_newActivity extends BaseActivity implements IOnHttpListener {
    private TextView tvTitle;
    private ImageView public_ll_return;
    private NetRequest netRequest;
    //掌上超市
    private TextView tv_zscs_zrxs;//昨日销售
    private TextView tv_zscs_byxs;//本月销售
    private TextView tv_zscs_ljxs;//累计销售
    //自动售货
    private TextView tv_zdsh_zrxs;//昨日销售
    private TextView tv_zdsh_byxs;//本月销售
    private TextView tv_zdsh_ljxs;//累计销售
    //惠商驿站
    private TextView hpt_openorclose;//伙拼团是否开通
    private TextView tv_hpt_ljdd;//累计订单
    private TextView tv_hpt_ljxs;//累计销售
    private TextView tv_hpt_ljsy;//累计收益
    private TextView wyp_openorclose;//我要批是否开通
    private TextView tv_wyp_zrxs;//昨日销售
    private TextView tv_wyp_byxs;//本月销售
    private TextView tv_wyp_ljxs;//累计销售
    //社区导购
    private TextView tv_sqdg_byxs;//本月销售
    private TextView tv_sqdg_ljxs;//累计销售
    private TextView tv_sqdg_ljfy;//累计返佣


    private SpecialSellRVAdapter adpter;
    private RecyclerView rv;
    private List<SpecialsellDeviceBean> specialsellDeviceBeanList;
    private RelativeLayout rl_zscs; //掌上超市
    private RelativeLayout rl_zdsh; //自动售货
    private RelativeLayout rl_hsyz; //惠商驿站
    private RelativeLayout rl_sqdg; //社区导购



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_special_sell_new2);
        ExitApplication.getInstance().addActivity(this);
        initView();
        init();
        initListeners();
    }

    private void initListeners() {
        public_ll_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.public_ll_right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent8 = new Intent();
                intent8.setClass(SpecialSell_newActivity.this, WebViewMoreActivity.class);
                intent8.putExtra("url", "http://help.meitianhui.com/?id=1008");
                startActivity(intent8);
            }
        });
    }

    private void initView() {
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvTitle.setText("特卖");
        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        tv_zscs_zrxs = (TextView) findViewById(R.id.tv_zscs_zrxs);
        tv_zscs_byxs = (TextView) findViewById(R.id.tv_zscs_byxs);
        tv_zscs_ljxs = (TextView) findViewById(R.id.tv_zscs_ljxs);
        tv_zdsh_zrxs = (TextView) findViewById(R.id.tv_zdsh_zrxs);
        tv_zdsh_byxs = (TextView) findViewById(R.id.tv_zdsh_byxs);
        tv_zdsh_ljxs = (TextView) findViewById(R.id.tv_zdsh_ljxs);
        hpt_openorclose = (TextView) findViewById(R.id.hpt_openorclose);
        tv_hpt_ljdd = (TextView) findViewById(R.id.tv_hpt_ljdd);
        tv_hpt_ljxs = (TextView) findViewById(R.id.tv_hpt_ljxs);
        tv_hpt_ljsy = (TextView) findViewById(R.id.tv_hpt_ljsy);
        wyp_openorclose = (TextView) findViewById(R.id.wyp_openorclose);
        tv_wyp_zrxs = (TextView) findViewById(R.id.tv_wyp_zrxs);
        tv_wyp_byxs = (TextView) findViewById(R.id.tv_wyp_byxs);
        tv_wyp_ljxs = (TextView) findViewById(R.id.tv_wyp_ljxs);
        tv_sqdg_byxs = (TextView) findViewById(R.id.tv_sqdg_byxs);
        tv_sqdg_ljxs = (TextView) findViewById(R.id.tv_sqdg_ljxs);
        tv_sqdg_ljfy = (TextView) findViewById(R.id.tv_sqdg_ljfy);
        findViewById(R.id.public_tv_right).setBackgroundResource(R.drawable.super_return_help);
        rv = (RecyclerView) findViewById(R.id.rv);
        initRecyclerView();
        rl_zscs = (RelativeLayout) findViewById(R.id.rl_zscs);
        rl_zscs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(SpecialSell_newActivity.this, PalmSupermarketActivity.class));
            }
        });
        rl_zdsh = (RelativeLayout) findViewById(R.id.rl_zdsh);
       /* rl_zdsh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SpecialSell_newActivity.this,AutomaticSellListActivity.class);
//                intent.putExtra("data",(Serializable) specialsellDeviceBeanList);
                intent.putParcelableArrayListExtra("data", (ArrayList<? extends Parcelable>) specialsellDeviceBeanList);

                startActivity(intent);
            }
        });*/
        rl_hsyz = (RelativeLayout) findViewById(R.id.rl_hsyz);
        rl_hsyz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        rl_sqdg = (RelativeLayout) findViewById(R.id.rl_sqdg);
        rl_sqdg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SpecialSell_newActivity.this, SpecialSell_ShoperActivity.class);
                startActivity(intent);
            }
        });
    }

    /**
     * 初始化显示终端设备列表的RecyclerView
     */
    private void initRecyclerView() {
        specialsellDeviceBeanList = new ArrayList<>();
//        specialsellDeviceBeanList.add(new SpecialsellDeviceBean());
//        specialsellDeviceBeanList.add(new SpecialsellDeviceBean());
//        specialsellDeviceBeanList.add(new SpecialsellDeviceBean());
//        specialsellDeviceBeanList.add(new SpecialsellDeviceBean());
//        specialsellDeviceBeanList.add(new SpecialsellDeviceBean());
//        specialsellDeviceBeanList.add(new SpecialsellDeviceBean());
        rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        //rv_show_goods_pic.addItemDecoration(new RecycleViewDivider(this,LinearLayoutManager.VERTICAL, Utils.dip2px(this,4), Color.RED));
        adpter = new SpecialSellRVAdapter(this, specialsellDeviceBeanList);
        rv.setAdapter(adpter);
    }

    private void init() {
        netRequest = new NetRequest(this);
        //获取掌上便利店数据
        netRequest.requestSpecialSell_ZSBLDQuery(1);
        //自动售货机数据
        netRequest.requestSpecialSell_AutoSellQuery(2);
        //伙拼团数据
        netRequest.requestSpecialSell_HPTQuery(3);
        //商品便利店上架商品数量
        netRequest.requestSpecialSell_SellGoodsCountForStoresSaleQuery(4);
        //我要批统计
        netRequest.requestSpecialSell_WYPQuery(5);
        //社区导购统计
        netRequest.requestSpecialSell_saleAssistantSaleCountQuery(6);
        //惠商驿站是否开通查询
        netRequest.requeststageStoresFind(8);
//        adpter=new Specialsell_newAdapter(this);
//        lv_zdsh.setAdapter(adpter);
//        Utils.Setlistview(lv_zdsh);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //  EventBus.getDefault().unregister(this);
    }


    @Override
    public void onAfinalFail() {
        showToast("请检查本地网络!");
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        Log.v("mylog", requestId + " " + result);
        if (requestId == 1) {
            //获取掌上便利店数据
            try {
                JSONObject data = new JSONObject(result);
                Log.v("zyl", "获取掌上便利店数据:" + data.toString());
                String s_yesterday_sell_amount = data.getString("yesterday_sell_amount");
                if (!TextUtils.isEmpty(s_yesterday_sell_amount)) {
                    tv_zscs_zrxs.setText(s_yesterday_sell_amount);//昨日销售
                }
                String s_month_sell_amount = data.getString("month_sell_amount");
                if (!TextUtils.isEmpty(s_month_sell_amount)) {
                    tv_zscs_byxs.setText(s_month_sell_amount);//本月销售
                }
                String s_totle_amount = data.getString("totle_amount");
                if (!TextUtils.isEmpty(s_totle_amount)) {
                    tv_zscs_ljxs.setText(s_totle_amount);//累计销售
                }
                String s_wait_delivery = data.getString("wait_delivery");

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestId == 2) {
            //自动售货

            try {
                JSONObject data = new JSONObject(result);
                String s_yesterday_sell_amount = data.getString("yesterday_sell_amount");
                if (!TextUtils.isEmpty(s_yesterday_sell_amount)) {
                    tv_zdsh_zrxs.setText(s_yesterday_sell_amount);//昨日销售
                }
                //终端设备
                String s_device_num = data.getString("device_num");
                String s_totle_amount = data.getString("totle_amount");
                if (!TextUtils.isEmpty(s_totle_amount)) {
                    tv_zdsh_ljxs.setText(s_totle_amount);//累计销售
                }
                String s_month_sell_amount = data.getString("month_sell_amount");
                if (!TextUtils.isEmpty(s_month_sell_amount)) {
                    tv_zdsh_byxs.setText(s_month_sell_amount);//本月销售
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestId == 3) {
            //伙拼团==>拼团领
            try {
                JSONObject data = new JSONObject(result);
                //参与会员
                String s_participate_member = data.getString("participate_member");
                String s_order_num = data.getString("order_num");
                if (!TextUtils.isEmpty(s_order_num)) {
                    tv_hpt_ljdd.setText(s_order_num);//累计订单
                }
                String s_totle_amount = data.getString("totle_amount");
                if (!TextUtils.isEmpty(s_totle_amount)) {
                    tv_hpt_ljxs.setText(s_totle_amount);//累计销售
                }
                String s_totle_earnings = data.getString("totle_earnings");
                if (!TextUtils.isEmpty(s_totle_earnings)) {
                    tv_hpt_ljsy.setText(s_totle_earnings);//累计收益
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestId == 4) {
            //掌上便利店 上架商品
            try {
                JSONObject data = new JSONObject(result);
                //上架商品
                String s_pis_sell_goods_count = data.getString("is_sell_goods_count");

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestId == 5) {
            //我要批统计
            try {
                JSONObject data = new JSONObject(result);
                //合作门店
                String s_cooperate_stores = data.getString("cooperate_stores");

                String s_yesterday_sell_amount = data.getString("yesterday_sell_amount");
                if (!TextUtils.isEmpty(s_yesterday_sell_amount)) {
                    tv_wyp_zrxs.setText(s_yesterday_sell_amount);//昨日销售
                }
                String s_month_sell_amount = data.getString("month_sell_amount");
                if (!TextUtils.isEmpty(s_month_sell_amount)) {
                    tv_wyp_byxs.setText(s_month_sell_amount);//本月销售
                }
                String s_totle_amount = data.getString("totle_amount");
                if (!TextUtils.isEmpty(s_totle_amount)) {
                    tv_wyp_ljxs.setText(s_totle_amount);//累计销售
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestId == 6) {
            //社区导购统计
            try {
                JSONObject data = new JSONObject(result);

                String s_cooperate_member = data.getString("cooperate_member");
                String s_month_sell_amount = data.getString("month_sell_amount");
                if (!TextUtils.isEmpty(s_month_sell_amount)) {
                    tv_sqdg_byxs.setText(s_month_sell_amount);//本月销售
                }
                String s_totle_amount = data.getString("totle_amount");
                if (!TextUtils.isEmpty(s_totle_amount)) {
                    tv_sqdg_ljxs.setText(s_totle_amount);//累计销售
                }
                String s_totle_rebate = data.getString("totle_rebate");
                if (!TextUtils.isEmpty(s_totle_rebate)) {
                    tv_sqdg_ljfy.setText(s_totle_rebate);//累计返佣
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestId == 7) {
        } else if (requestId == 8) {
            //惠商驿站是否开通查询
            //{"is_stage_hpt":"N","is_stage_hyd":"N"}


            try {
                JSONObject jsonObject = new JSONObject(result);
                String is_stage_hpt = jsonObject.getString("is_stage_hpt");
                String is_stage_hyd = jsonObject.getString("is_stage_hyd");
                if (is_stage_hpt.equals("Y")) {
                    hpt_openorclose.setText("开通");
                } else {
                    hpt_openorclose.setText("关闭");
                }
                if (is_stage_hyd.equals("Y")) {
                    wyp_openorclose.setText("开通");
                } else {
                    wyp_openorclose.setText("关闭");
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void onExecuteFail(int requestId, String msg) {

    }
}
